/* eslint-disable vue/no-parsing-error */
<template>
  <div class="uploadimg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="value"
        v-imgerror="errirImg"
        :src="value"
        class="avatar"
        @click.stop="isShow = true"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 关闭按钮 -->
    <div v-if="value" class="close" @click="del">x</div>
    <!-- 查看大图 -->
    <el-dialog :visible.sync="isShow" title="大图预览" width="400px">
      <img style="width: 100%" :src="value" alt="">
    </el-dialog>
    <transition name="fade">
      <el-progress
        v-if="showProgress"
        :percentage="percentage"
        style="width: 210px; padding-top: 10px"
        status="success"
      />
    </transition>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDue8LHyQbV9XeU8XhaCWZLq42jAKErDI6',
  SecretKey: 'ZrsgCRP6WgYAVZMr77pGQJvGJ6dy4vVo'
})
export default {
  name: 'Uploadimg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      percentage: 0,
      isShow: false,
      showProgress: false,
      // require读取路径
      errirImg: require('@/assets/common/head.jpg')
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是JPG,PNG格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过2NB!')
        return isJPG && isLt2M
      }
    },
    httpRequest(data) {
      this.percentage = 0
      this.showProgress = true
      cos.putObject(
        {
          Bucket: '2021-11-5-1308138787' /* 存储桶*/,
          Region: 'ap-guangzhou' /* 存储桶所在地域,必须字段*/,
          Key: data.file.uid + '', // 标识
          StorageClass: 'STANDARD', // 固定值
          Body: data.file, // 上传文件对象  fi1e文件对象
          onProgress: (progressData) => {
            // 上传进度,实时返回上传文件进度
            this.percentage = +(progressData.percent * 100).toFixed(1)
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showProgress = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (!err) {
            this.$emit('input', 'http://' + data.Location)
          }
        }
      )
    },
    // 删除
    del() {
      this.$emit('input', '')
    }
  }
}
// 自定义上传
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  &:hover {
    border-color: #409eff;
  }
}
// ::v-deep .avatar-uploader .el-upload:hover {
//   border-color: #409eff;
// }
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadimg {
  // border: 1px dashed #d9d9d9;
  position: relative;
  width: 178px;
  height: 180px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    position: absolute;
    top: 5px;
    right: 10px;
    cursor: pointer;
    display: none;
  }
}
</style>
